package com.example.demo.controller.admin;

import com.example.demo.Util.Result;
import com.example.demo.Util.ResultUtil;
import com.example.demo.mapper.ClassMap;
import com.example.demo.mapper.ClassMap;
import com.example.demo.po.Class;
import com.example.demo.po.dto.ClassParam;
import com.example.demo.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/class")
public class ClassController {

    @Autowired
    ClassMap classMap;

    @Autowired
    ClassService classService;

    @GetMapping("/get")
    @ResponseBody
    public Result GetClass(){
        List<Class> ClassList = new ArrayList();
        ClassList = classMap.GetClass();

        return ResultUtil.success(ClassList);
    }

    @GetMapping("/search")
    @ResponseBody
    public Result SearchClass( @RequestParam("name")  String name){

        if(name == null|| name.isEmpty())
        {
            return ResultUtil.success(classMap.GetClass()) ;
        }
        List<Class> ClassList = new ArrayList();
        ClassList = classMap.GetClassByName("%"+name+"%");
        return ResultUtil.success(ClassList);
    }

    @PostMapping("/save")
    @ResponseBody
    public Result SaveClass(@RequestBody ClassParam _class){

      Result res = classService.SaveClass(_class);

        return res;
    }

    @GetMapping("/delete/{id}")
    @ResponseBody
    public Result DeleteClass(@PathVariable int id){
        classMap.DeleteClass(id);
        return ResultUtil.success("删除成功");
    }

    @PostMapping("/update")
    public Result UpdateClass(@RequestBody ClassParam classpo){

         classService.UpdateClass(classpo);
         return ResultUtil.success("更新成功");
    }

}
